package com.wdl.fmnew.core.net;

import java.util.concurrent.TimeUnit;

import javax.inject.Named;
import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by wdl on 2017/9/18.
 * di module
 */
@Module
public class NetModule {
    @Singleton
    @Provides
    @Named("default")
    public OkHttpClient provideDefaultHttpClient() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(NetConfig.DEFAULT_TIME_OUT, TimeUnit.SECONDS);//连接超时时间
        builder.writeTimeout(NetConfig.DEFAULT_TIME_OUT, TimeUnit.SECONDS);//写操作 超时时间
        builder.readTimeout(NetConfig.DEFAULT_TIME_OUT, TimeUnit.SECONDS);//读操作超时时间

        // 添加公共参数拦截器
        BasicParamsInterceptor basicParamsInterceptor = new BasicParamsInterceptor.Builder()
                .addParam("key", "082e6ce4c248d92c4a597123d55e29b0")//添加公共参数
                .addParam("os", "1")
                .build();

        builder.addInterceptor(basicParamsInterceptor);

        return builder.build();
    }

    @Singleton
    @Provides
    @Named("default")
    public Retrofit providesDefaultRetrofit(@Named("default") OkHttpClient okHttpClient) {
        return new Retrofit.Builder()
                .baseUrl(NetConfig.BASE_URL)
                .client(okHttpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
    }
}
